package com.workflow.engine.eb.steps.al;

import com.workflow.engine.core.flow.StepState;
import com.workflow.engine.parser.annotation.PGConfig;
import com.workflow.engine.parser.constant.HttpMethod;
import com.workflow.engine.parser.flow.impl.AbstractStep;
import com.workflow.engine.parser.utils.JsoupHelper;
import com.workflow.engine.parser.utils.StepStateUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;

import static com.workflow.engine.parser.constant.HttpMethod.GET;

/**
 * 腾讯登录请求
 * Created by houjinxin on 2017/2/23.
 */
@PGConfig({})
public class AlipayGetInfo extends AbstractStep  {

    private static final Logger logger = LoggerFactory.getLogger(AlipayGetInfo.class);

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    protected String getRequestUrl() {
        return "https://auth.alipay.com/login/index.htm";
    }

    @Override
    protected HttpMethod getHttpMethod() {
        return GET;
    }

    @Override
    protected Map<String, String> getHeaders() {
        return new LinkedHashMap<String, String>(){{
            put("Host", "auth.alipay.com");
            put("Connection", "keep-alive");
            put("Cache-Control", "max-age=0");
            put("Upgrade-Insecure-Requests", "1");
            put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
            put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            put("Accept-Encoding", "gzip, deflate, sdch, br");
            put("Accept-Language", "zh-CN,zh;q=0.8");
        }};
    }

    @Override
    protected StepState handleResponse(Map<String, Object> context, Object response) throws Exception {
        logger.info("结果：{}", response);
        Document doc = Jsoup.parse((String) response);

        String ua = "";
        String support = JsoupHelper.getElementsByName(doc, "support").val();
        String superSwitch = JsoupHelper.getElementsByName(doc, "superSwitch").val();
        String sso_hid = JsoupHelper.getElementsByName(doc, "sso_hid").val();
        String site = JsoupHelper.getElementsByName(doc, "site").val();
        String security_activeX_enabled = JsoupHelper.getElementsByName(doc, "security_activeX_enabled").val();
        String REMOTE_PCID_NAME = JsoupHelper.getElementsByName(doc, "REMOTE_PCID_NAME").val();
        String redirectType = JsoupHelper.getElementsByName(doc, "redirectType").val();
        String rds_form_token = JsoupHelper.getElementsByName(doc, "rds_form_token").val();
        String qrCodeSecurityId = JsoupHelper.getElementsByName(doc, "qrCodeSecurityId").val();
        String preCheckTimes = JsoupHelper.getElementsByName(doc, "preCheckTimes").val();
        String personalLoginError = JsoupHelper.getElementsByName(doc, "personalLoginError").val();
        String passwordSecurityId = JsoupHelper.getElementsByName(doc, "passwordSecurityId").val();

        //TODO:js生成
        String password_rsainput = "";

        String password_input = JsoupHelper.getElementsByName(doc, "password_input").val();
        String password = JsoupHelper.getElementsByName(doc, "password").val();

        String noActiveX = JsoupHelper.getElementsByName(doc, "noActiveX").val();
        String needTransfer = JsoupHelper.getElementsByName(doc, "needTransfer").val();
        String method = JsoupHelper.getElementsByName(doc, "method").val();
        String logonId = JsoupHelper.getElementsByName(doc, "logonId").val();
        String loginScene = JsoupHelper.getElementsByName(doc, "loginScene").val();
        String json_tk = JsoupHelper.getElementsByName(doc, "json_tk").val();
        String J_aliedit_using = JsoupHelper.getElementsByName(doc, "J_aliedit_using").val();
        String J_aliedit_uid_hidn = JsoupHelper.getElementsByName(doc, "J_aliedit_uid_hidn").val();
        String J_aliedit_prod_type = JsoupHelper.getElementsByName(doc, "J_aliedit_prod_type").val();
        String J_aliedit_net_info = JsoupHelper.getElementsByName(doc, "J_aliedit_net_info").val();
        String J_aliedit_key_hidn = JsoupHelper.getElementsByName(doc, "J_aliedit_key_hidn").val();
        String idPrefix = JsoupHelper.getElementsByName(doc, "idPrefix").val();
        String $goto = JsoupHelper.getElementsByName(doc, "goto").val();
        String errorVM = JsoupHelper.getElementsByName(doc, "errorVM").val();
        String errorGoto = JsoupHelper.getElementsByName(doc, "errorGoto").val();
        String CtrlVersion = JsoupHelper.getElementsByName(doc, "CtrlVersion").val();
        //验证码
        String checkCode = JsoupHelper.getElementsByName(doc, "checkCode").val();
        String alieditUid = JsoupHelper.getElementById(doc, "alieditUid").val();
        String _seaside_gogo_pcid = JsoupHelper.getElementsByName(doc, "_seaside_gogo_pcid").val();
        String _seaside_gogo_p = JsoupHelper.getElementsByName(doc, "_seaside_gogo_p").val();
        String _seaside_gogo_ = JsoupHelper.getElementsByName(doc, "_seaside_gogo_").val();

        context.put("ua", ua);
        context.put("support", support);
        context.put("superSwitch", superSwitch);
        context.put("sso_hid", sso_hid);
        context.put("site", site);
        context.put("security_activeX_enabled", security_activeX_enabled);
        context.put("REMOTE_PCID_NAME", REMOTE_PCID_NAME);
        context.put("redirectType", redirectType);
        context.put("rds_form_token", rds_form_token);
        context.put("qrCodeSecurityId", qrCodeSecurityId);
        context.put("preCheckTimes", preCheckTimes);
        context.put("personalLoginError", personalLoginError);
        context.put("passwordSecurityId", passwordSecurityId);
        context.put("password_rsainput", password_rsainput);
        context.put("password_input", password_input);
        context.put("password", password);

        context.put("noActiveX", noActiveX);
        context.put("needTransfer", needTransfer);
        context.put("method", method);
        context.put("logonId", logonId);
        context.put("loginScene", loginScene);
        context.put("json_tk", json_tk);
        context.put("J_aliedit_using", J_aliedit_using);
        context.put("J_aliedit_uid_hidn", J_aliedit_uid_hidn);
        context.put("J_aliedit_prod_type", J_aliedit_prod_type);
        context.put("J_aliedit_net_info", J_aliedit_net_info);
        context.put("J_aliedit_key_hidn", J_aliedit_key_hidn);
        context.put("idPrefix", idPrefix);
        context.put("goto", $goto);
        context.put("errorVM", errorVM);
        context.put("errorGoto", errorGoto);
        context.put("CtrlVersion", CtrlVersion);
        context.put("checkCode", checkCode);
        context.put("alieditUid", alieditUid);
        context.put("_seaside_gogo_pcid", _seaside_gogo_pcid);
        context.put("_seaside_gogo_p", _seaside_gogo_p);
        context.put("_seaside_gogo_", _seaside_gogo_);
        return StepStateUtil.successfulStepState(this);
    }
}
